def hanoi(n, source, target, auxiliary):
    if n > 0:
        hanoi(n - 1, source, auxiliary, target)

        print(f"移动盘子 {n} 从 {source} 到 {target}")

        hanoi(n - 1, auxiliary, target, source)

hanoi(3, 'A', 'C', 'B')


def hanoi_visual(n, source='A', target='C', auxiliary='B'):
    towers = {source: list(range(n, 0, -1)), target: [], auxiliary: []}
    print("初始状态：", towers)

    def move_disk(from_tower, to_tower):
        disk = towers[from_tower].pop()
        towers[to_tower].append(disk)
        print(f"移动盘子 {disk} 从  {from_tower} 到 {to_tower}")
        print("当前状态：", towers)


    def hanoi_recursive(n, source, target, auxiliary):
        if n >0:
            hanoi_recursive(n - 1, source, auxiliary, target)
            move_disk(source, target)
            hanoi_recursive(n - 1, auxiliary, target, source)
        hanoi_recursive(n, source, target, auxiliary)

hanoi_visual(3)
import turtle

def hanoi_turtle(n):
    screen = turtle.Screen()
    screen.setup(800,600)
    screen.title("汉诺塔可视化")

    t = turtle.Turtle()
    t.speed(0)
    t.hideturtle()

    for i, pos in enumerate([-200, 0, 200]):
        t.penup()
        t.goto(pos, -100)
        t.pendown()
        t.setheading(90)
        t.forward(200)
        t.write(f"{chr(65 + i)}柱", align="center")



    disks = []
    for i in range(n):
        d = turtle.Turtle()
        d.shape("square")
        d.shapesize(1, (n - i) * 1.5)
        d.color("blue")
        d.penup()
        d.goto(-200, -90 + i * 20)
        disks.append(d)

    def move_disk_visual(disk_num, from_pos, to_pos):
        disk = disks[disk_num - 1]
        y_pos = len([d for d in disks if d.xcor() == to_pos]) * 20 -90

        for _ in range(10):
            disk.setu(disk.ycor() + 5)
            turtle.update()
            turtle.delay(50)

        step = (to_pos - from_pos) / 20
        for _ in range(20):
            disk.setx(disks.xcor() + step)
            turtle.update()
            turtle.delay(50)

        for _ in range(10):
            disk.sety(disk.ycor() - 5)
            turtle.update()
            turtle.delay(50)

    def hanoi_recursive(n, source, target, auxiliary):
        if n > 0:
            hanoi_recursive(n - 1, source, auxiliary, target)
            move_disk_visual(n, source, target)
            hanoi_recursive(n -1, auxiliary, target, source)

    turtle.target(0, 0)
    hanoi_recursive(n, -200,200,0)
    turtle.done()


hanoi_turtle(4)

import os


def traverse_directory(path, indent=0):
    """递归遍历文件夹结构"""
    print(' ' * indent + f"[{os.path.basename(path)}]")


    try:
        for item in os.listdir(path):
            item_path = os.path.join(path, item)

            if os.path.isdir(item_path):

                traverse_directory(item_path, indent + 4)
            else:

                print(' ' * (indent + 4) + item)
    except PermissionError:
        print(' ' * indent + "!权限不足，无法访问")


traverse_directory(',')

def traverse_with_stats(path, indent=0):
    """带统计信息的文件夹遍历"""
    total_files = 0
    total_size = 0

    print(' ' * indent + f"+ {os.path.basename(path)}/")

    try:
        items = os.listdir(path)
        for item in  items:
            item_path= os.path.join(path, item)

            if os.path.isdir(item_path):
                sub_files, sub_size = traverse_with_stats(item_path, indent + 4)
                total_files += sub_files
                total_size += sub_size
            else:
                file_size = os.path.getsize(item_path)
                print(' ' * (indent + 4) + f"- {item} ({file_size} bytes)")
                total_files += 1
                total_size += file_size
    except PermissionError:
        print(' ' * (indent + 4) + "!权限不足")

    print(' ' * indent + f" [总计： {total_files}个文件， {total_size / 1024:.1f} KB]")
    return total_files, total_size

traverse_with_stats(',')

import os
import hashlib

def find_duplicates(start_path):
    """查找重复文件"""
    seen_files = {}
    duplicates = []

    for root, dirs, files in os.walk(start_path):
        for filename in files:
            filepath = os.path.join(root, filename)

            try:
                with open(filepath, 'rb') as f:
                    file_hash = hashlib.md5(f.read()).hexdigest()

                if file_hash in seen_files:
                    duplicates.append((filepath, seen_files[file_hash]))
                else:
                    seen_files[file_hash] = filepath
            except (IOError, PermissionError):
                continue
    return duplicates

dups = find_duplicates(',')
for dup in dups:
    print(f"重复文件： {dup[0]} 和 {dup[1]}")





